
# -*- coding: utf-8 -*-
from django.shortcuts import (render, redirect,
                              get_object_or_404, get_list_or_404)
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.shortcuts import render_to_response, get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext


from .models import *

def showlist(request):
	#from cmsApp.models import News2015 as rNews2015
	#rs=rNews2015.noclearAndSync_30()
	#rchf_online_users.
	#return u"恭喜您，登录成功"
	if request.META.has_key('HTTP_X_FORWARDED_FOR'):
		ip =  request.META['HTTP_X_FORWARDED_FOR']
	else:
		ip = request.META['REMOTE_ADDR']

	msg_ip=u'您的IP地址:'+ip

	wenjuan_id=123
	wjarray=wenjuan.objects.filter(status=0);
	return render(request, 'wenjuan/list.html', {'wenjuan_id': wenjuan_id,'wjarray':wjarray,'tnumber':len(wjarray)})

import time
def showitem(request, wenjuan_id):
	if request.META.has_key('HTTP_X_FORWARDED_FOR'):
		ip =  request.META['HTTP_X_FORWARDED_FOR']
	else:
		ip = request.META['REMOTE_ADDR']

	wjitem=get_object_or_404(wenjuan,pk=wenjuan_id)
	tmarray=get_list_or_404(timu,wenjuan=wjitem)
	#用于判断是否是反复提交的数据
	wjkey=str(time.time())+ip
	print tmarray
	return render(request, 'wenjuan/item.html', {'wjkey':wjkey,'wenjuan_id': wenjuan_id,'tmarray':tmarray,'tnumber':len(tmarray)})


def jieguofun(request):
	if request.method == 'GET':
		return HttpResponseRedirect('/wenjuan/list/')
	if request.method == 'POST':
		print request
		wjid = request.POST.get('wjid','')
		wjkey= request.POST.get('wjkey','')
		if (wjid == '') or (wjkey==''):
			errors=[]
			errors.append(u'抱歉您传递的参数不正确，请联系网站管理员')
			return render(request, 'wenjuan/error.html', {'errors':errors})
		elif checkWJKEY(wjkey)==1:
			errors=[]
			errors.append(u'抱歉,请不要反复提交相同的数据')
			return render(request, 'wenjuan/error.html', {'errors':errors})
		else:
			if request.META.has_key('HTTP_X_FORWARDED_FOR'):
				ip =  request.META['HTTP_X_FORWARDED_FOR']
			else:
				ip = request.META['REMOTE_ADDR']
			msg_ip=u'您的IP地址:'+ip
			wjitem=get_object_or_404(wenjuan,pk=int(wjid))
			#request.POST.pop('csrfmiddlewaretoken')
			valueDict=clearValueDict(request.POST)
			ys=yuanshiJieguo(wjkey=wjkey,username=ip,ipaddress=ip,wenjuanItem=wjitem,value=str(valueDict))
			ys.save()
			operateWenjuanValue(valueDict,wjkey,ip)
			return render(request, 'wenjuan/finish.html', {})

	return HttpResponse('ok')


#-------------------------
#清除post数据中不必要的参数
#-------------------------
def clearValueDict(vPostData):
	value=dict(vPostData)
	value.pop('csrfmiddlewaretoken')
	value.pop('wjid')
	value.pop('wjkey')
	return value


#-------------------------
#检测问卷数据是否已经提交过了
#-------------------------
def checkWJKEY(wjkey):
	wArray=yuanshiJieguo.objects.filter(wjkey=wjkey)
	if len(wArray)==0:
		return 0
	else:
		return 1;

#================================================================
#---------------------------------
#'1_2_3' 转化为 问卷，题号，题目ID
#---------------------------------	
def nameToWXT(oneStr):
	rs=oneStr.split('_')
	if len(rs)==3:
		return 0,int(rs[0]),int(rs[1]),int(rs[2])	
	return 1,0,0,0


#-------------------------
#处理表单参数存入数据库
#-------------------------	
def operateWenjuanValue(valueDict,wjkey,ip):
	for k,v in valueDict.items():
		onelist=nameToWXT(k)
		onerow={}
		if (onelist[0]==0) and (len(v)==1):
			ret,onerow['wenjuanItemID'],onerow['shunxuID'],onerow['timuItemID']=onelist
			onerow['wjkey']=wjkey
			onerow['ipaddress']=ip
			onerow['value']=v[0]
			onerow['username']=''
			cfItem=chafenJieguo(wjkey=onerow['wjkey'],username='',
				ipaddress=onerow['ipaddress'],wenjuanItemID=onerow['wenjuanItemID'],
				timuItemID=onerow['timuItemID'],shunxuID=onerow['shunxuID'],value=onerow['value'])
			cfItem.save()



	


